import React, {useEffect, useState} from 'react'
import {Button, Space} from 'antd'
import {useLocation} from 'umi'
import Basis from './basis'
import CarInfo from './carInfo'
import CheckCarInfo from './checkCarInfo'
import Contract from './contract'
import DeliveryCarInfo from './deliveryCarInfo'
import OtherOrder from './otherOrder'
import RenewInfo from './renewInfo'
import ExchangeCarInfo from '../exchangeCarInfo'
import ReturnCheckCar from '../../returnCar/returnCheckCar'
import CarHistory from './carHistory'
import {scrollToAnchor} from '@/utils'
import styles from './index.less'
import {
	getTransferDetails,
	getReturnDetails,
	getTransfer,
	getRenewalRecord,
	getCarHistory
} from '@/service/order'
import TransFerInfo from './transferInfo'

function Details(props: any) {
	const {info} = props
	const {
		query: {type},
	}: any = useLocation()
	const [changeCars, setChangeCars] = useState(2) // 换车状态
	// const [returnCars,setReturnCars] = useState(2) // 退车状态
	const [returnStatus, setReturnStatus] = useState(2) // 退车缴费
	// const [returnInfo,setReturnInfo] = useState() // 退车信息
	const [transfer, setTransfer] = useState(2) // 过户信息
	const [tenewalStatus, setRenewalStatus] = useState(2)
	const [chanCarHistory,setChanCarHistory] = useState(2) // 换车历史记录
	useEffect(() => {
		if (type) {
			scrollToAnchor(type)
		}
	})
	useEffect(() => {
		// 换车记录
		if(info?.id){	
			const data={
				current:1,
				size:10,
				orderCarId:info?.id
			}
			getCarHistory(data).then((res:any)=>{
				if (res.success) {
					if (res?.data?.records?.length>0) {
						setChanCarHistory(1)
					} else {
						setChanCarHistory(2)
					}
				}
			})
		}
		// 有没有换车信息
		if (info?.id) {
			getTransferDetails(info?.id).then((res: any) => {
				if (res.success) {
					if (res?.data?.quondamOrderCarId) {
						setChangeCars(1)
					} else {
						setChangeCars(2)
					}
				}
			})
		}
		// 退车信息
		if (info?.id) {
			getReturnDetails(info?.id).then((res: any) => {
				if (res?.success) {
					if (res?.data?.returnNo) {
						setReturnStatus(1)
					} else {
						setReturnStatus(2)
					}
					// if(res?.data?.validateStatus){
					//   setReturnStatus(1)
					// }else{
					//   setReturnStatus(2)
					// }
					// if(res?.data?.validateStatus === 1){
					//   // 已经验车
					//   if(res?.data?.settlementStatus === 1 || res?.data?.settlementStatus === 3 ||res?.data?.settlementStatus === 9){
					//     // 已经结算
					//     setReturnStatus(1)
					//   }
					// }else{
					//   setReturnStatus(2)
					// }
				}
			})
		}
		// 过户信息
		if (info?.id) {
			getTransfer(info?.id).then((res: any) => {
				if (res?.success) {
					if (res?.data?.transferNo) {
						setTransfer(1)
					} else {
						setTransfer(2)
					}
				}
			})
		}
		// 续签信息
		const queryDataXQ = {
			pristineOrderCarId: info?.id,
		}
		const queryData = {
			quondamOrderCarId: info?.id,
		}
		getRenewalRecord(info?.isRelet === 1 ? queryDataXQ : queryData).then(
			(res: any) => {
				if (res?.success) {
					if (res?.data?.total > 0) {
						setRenewalStatus(1)
					} else {
						setRenewalStatus(2)
					}
				}
			},
		)
	}, [info])
	return (
		<>
			<div className={styles.btnList}>
				{info && (
					<Space>
						{/*  */}
						<Button onClick={() => scrollToAnchor('basis')}>
							基础信息
						</Button>
						{info?.vinNo && (
							<Button onClick={() => scrollToAnchor('carInfo')}>
								绑车信息
							</Button>
						)}
						{info?.validate && (
							<Button
								onClick={() => scrollToAnchor('checkCarInfo')}>
								{' '}
								验车信息
							</Button>
						)}
						{info?.contract && (
							<Button onClick={() => scrollToAnchor('contract')}>
								合同信息
							</Button>
						)}
						{info?.delivery && (
							<Button
								onClick={() =>
									scrollToAnchor('deliveryCarInfo')
								}>
								交车信息
							</Button>
						)}
						{tenewalStatus === 1 && (
							<Button onClick={() => scrollToAnchor('renewInfo')}>
								续签信息
							</Button>
						)}
						{changeCars === 1 && (
							<Button
								onClick={() => scrollToAnchor('exchangeCar')}>
								{' '}
								换车信息
							</Button>
						)}
						{returnStatus === 1 && (
							<Button onClick={() => scrollToAnchor('returnCar')}>
								退车信息
							</Button>
						)}
						{transfer === 1 && (
							<Button onClick={() => scrollToAnchor('transfer')}>
								过户信息
							</Button>
						)}
						{chanCarHistory === 1 && (
							<Button onClick={() => scrollToAnchor('carHistory')}>
								换车历史记录
							</Button>
						)}
						<Button onClick={() => scrollToAnchor('otherOrder')}>
							其他关联单据信息
						</Button>
					</Space>
				)}
			</div>
			{info && (
				<>
					{/* 基础信息 */}
					<Basis info={info} />
					{/* 绑车信息 */}
					{info?.vinNo && <CarInfo info={info} />}
					{/* 验车信息 */}
					{info?.validate && <CheckCarInfo info={info} />}

					{/* 合同信息 */}
					{info?.contract && <Contract info={info} />}

					{/* 交车信息 */}
					{info?.delivery && <DeliveryCarInfo info={info} />}

					{/* 续签信息 */}
					{/* <RenewInfo info={info}/> */}
					{tenewalStatus === 1 && <RenewInfo info={info} />}

					{/* 换车信息展示，含有费用输入；如果订单没有换车操作，则不展示 */}
					{/* <ExchangeCar  info={info}/>     */}
					{changeCars === 1 && (
						<ExchangeCarInfo info={info} type="look" />
					)}

					{/* 退车验车 */}
					{/* {returnCars===1 && <ReReturnCarInfo info={returnInfo}/>} */}
					{/* 退车结算 */}
					{returnStatus === 1 && (
						<ReturnCheckCar info={info} type="look" state="pay" />
					)}
					{/* 换车历史记录 */}
					{chanCarHistory === 1 && (
						<CarHistory info={info}  />
					)}
						
					{/* 过户信息 */}
					{transfer === 1 && <TransFerInfo info={info} />}
					{/* 其他关联单据信息 */}
					<OtherOrder info={info} />
				</>
			)}
		</>
	)
}

export default Details
